package com.sd.repay.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.sd.repay.annotation.ClientVersionCheck;
import com.sd.repay.common.CommonResult;
import com.sd.repay.common.Constants;
import com.sd.repay.service.ClientVersionService;
import com.sd.repay.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Map;

@Component
public class ClientVersionCheckInterceptor extends HandlerInterceptorAdapter {
    private static Logger log = LoggerFactory.getLogger(ClientVersionCheckInterceptor.class);
    @Autowired
    private ClientVersionService clientVersionService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            //获得注解对象
            ClientVersionCheck clientVersionCheck = ((HandlerMethod) handler).getMethodAnnotation(ClientVersionCheck.class);
            Method method = ((HandlerMethod) handler).getMethod();

            String fullContentType = "application/json;charset=UTF8";
            response.setCharacterEncoding("UTF-8");
            response.setContentType(fullContentType);

            if (clientVersionCheck != null) {
                String osType = request.getParameter("osType");
                String osVersion = request.getParameter("osVersion");
                String clientVersion = request.getParameter("clientVersion");

                if (StringUtil.isBlank(osType, osVersion, clientVersion)) {
                    response.getWriter().print(JSONObject.toJSONString(CommonResult.build(403, "必要参数不能有空值")));
                    return false;
                }
                Map<String, Object> clientVersionMap = clientVersionService.getClientVersion(osType, clientVersion);
                if (clientVersionMap == null) {
                    response.getWriter().print(JSONObject.toJSONString(CommonResult.build(403, "客户端版本异常")));
                    return false;
                }
                String isNew = (String) clientVersionMap.get("is_new");
                if (!Constants.CLIENT_VERSION_IS_NEW.equals(isNew)) {
                    Long currIndex = (Long) clientVersionMap.get("id");
                    Map<String, Object> newClientVersionMap = clientVersionService.getMustClientVersion(osType, currIndex, Constants.CLIENT_VERSION_DOWN_MUST);
                    if (newClientVersionMap != null) {
                        response.getWriter().print(JSONObject.toJSONString(CommonResult.build(403, "请更新客户端后再试")));
                        return false;
                    }
                }
            }
        }
        return true;
    }

}
